#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <atomic>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    const std::string temp_path = "./temp/";
    class TimeUtil
    {
    public:
        static std::string GetTimeStamp()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec);
        }
        // 获得毫秒时间戳
        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }
    };
    // 处理文件名
    class PathUtil
    {
    public:
        // 编译时临时文件
        static std::string AddSuffix(const std::string &fileName, const std::string &suffix)
        {
            return temp_path + fileName + suffix;
        }
        static std::string Src(const std::string &fileName)
        {
            return AddSuffix(fileName, ".cpp");
        }
        static std::string Exe(const std::string &fileName)
        {
            return AddSuffix(fileName, ".exe");
        }
        static std::string CompilerError(const std::string &fileName)
        {
            return AddSuffix(fileName, ".compile_error");
        }
        // 运行时临时文件
        static std::string Stdin(const std::string &fileName)
        {
            return AddSuffix(fileName, ".stdin");
        }
        static std::string Stdout(const std::string &fileName)
        {
            return AddSuffix(fileName, ".stdout");
        }
        static std::string Stderr(const std::string &fileName)
        {
            return AddSuffix(fileName, ".stderr");
        }
    };

    class FileUtil
    {
    public:
        static bool IsFileExists(const std::string &filePath)
        {
            struct stat _stat;
            if (stat(filePath.c_str(), &_stat) == 0)
                return true;
            return false;
        }

        static std::string UniqFileName()
        {
            static std::atomic_uint id(0);
            id++;
            // 毫秒级时间戳+原子性递增唯一值: 来保证唯一性
            std::string ms = TimeUtil::GetTimeMs();
            std::string uniq_id = std::to_string(id);
            return ms + "_" + uniq_id;
        }
        static bool WriteFile(const std::string &target, const std::string &content)
        {
            std::ofstream out(target);

            if (!out.is_open())
            {
                return false;
            }

            out.write(content.c_str(), content.size());

            out.close();
            return true;
        }
        static bool ReadFile(const std::string &target, std::string *content, bool keep = false)
        {
            content->clear();
            std::ifstream in(target);

            if (!in.is_open())
            {
                return false;
            }

            std::string line;
            while (std::getline(in, line))
            {
                // getline:不保存行分割符,有些时候需要保留\n,
                // getline内部重载了强制类型转化
                (*content) += line;
                (*content) += keep ? "\n" : "";
            }
            in.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void SplitString(const std::string &line, std::vector<std::string> *target, const std::string &sep)
        {
            //boost库的字符串分割
            boost::algorithm::split((*target), line, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
};